package com.amigo.online.provider.manager.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


public class ParamterNullCheck {
    public static ParamterNullCheck instance = new ParamterNullCheck();

    /**
     * 判断传来的数据是否为空
     *
     * @param object    key
     * @param paramters 定义字符串key
     * @return
     */

    public JSONObject checkNull(Object object, String[] paramters) {
        JSONObject param = null;
        JSONObject result = new JSONObject();
        if (!(object instanceof JSONObject)) {
            param = (JSONObject) JSON.toJSON(object);
        } else {
            param = (JSONObject) object;
        }
        if (paramters != null) {
            for (String key : paramters) {
                Object value = param.get(key);
                if (value == null || "".equals(value)) {
                    result.put("msg", Constants.SYS_PARAMTER_MISSING);
                    return result;
                } else if (value instanceof JSONArray) {
                    if (((JSONArray) value).size() == 0) {

                        result.put("msg", Constants.SYS_PARAMTER_MISSING);
                        return result;
                    }
                }
            }
        }
        return null;

    }

    public static ParamterNullCheck getInstance() {
        return instance;
    }

    public static void setInstance(ParamterNullCheck instance) {
        ParamterNullCheck.instance = instance;
    }

}
